﻿using System;
using System.Collections.Generic;
using System.Net.Mail;
using System.Configuration;
using System.Net;

namespace Email
{
    /// <summary>
    /// EmailClient Class
    /// </summary>
    public class EmailClient
    {
        #region Parameters
        private string host = "";
        private int port = 25;
        private MailMessage mailMessage = new MailMessage();
        private SmtpClient smtpClient;
        private string smtpUserName = string.Empty;
        private string smtpPassword = string.Empty;
        #endregion

        #region Constructor
        /// <summary>
        /// EmailClient Constructor
        /// Do some Configuration stuff
        /// Contain Setting:SmtpServer,SmtpPort[,SmtpUserName,SmtpPassword]
        /// </summary>
        public EmailClient()
        {
            this.host = ConfigurationManager.AppSettings["SmtpServer"];
            string pt = ConfigurationManager.AppSettings["SmtpPort"];
            smtpUserName = ConfigurationManager.AppSettings["SmtpUserName"];
            smtpPassword = ConfigurationManager.AppSettings["SmtpPassword"];
            if (string.IsNullOrEmpty(host))
            {
                throw new ConfigurationErrorsException("Missing SMTP host Configuration!");
            }
            if (!string.IsNullOrEmpty(pt))
            {
                if (!int.TryParse(pt, out this.port))
                {
                    throw new ConfigurationErrorsException("Missing SMTP port Configuration!");
                }
            }
            else
            {
                this.port = 25;
            }
            this.smtpClient = new SmtpClient(host, port);
            this.smtpClient.UseDefaultCredentials = true;

            if (!string.IsNullOrEmpty(this.smtpUserName) && !string.IsNullOrEmpty(this.smtpPassword))
            {
                smtpClient.Credentials = new System.Net.NetworkCredential(smtpUserName, smtpPassword);
            }
            else
            {
                smtpClient.Credentials = CredentialCache.DefaultNetworkCredentials;
            }
        }
        #endregion

        #region Methods
        /// <summary>
        /// Send Email
        /// </summary>
        /// <param name="from">From Address</param>
        /// <param name="to">To Address</param>
        /// <param name="cc">Cc Address</param>
        /// <param name="re">Re Address</param>
        /// <param name="subject">The subject of the mail</param>
        /// <param name="body">Mail's body</param>
        /// <param name="attachmentList">Mail's Attachment List</param>
        /// <param name="isHtml">If mail's body format HTML</param>
        /// <returns>Sending result</returns>
        public bool Send(string from, string to, string cc, string re, string subject, string body, List<Attachment> attachmentList, bool isHtml)
        {
            try
            {
                this.mailMessage.From = new MailAddress(from);

                char[] c = { ',', ';' };

                string[] strTo = to.Split(c);
                if (strTo.Length > 0)
                {
                    foreach (string strTmp in strTo)
                    {
                        if (!string.IsNullOrEmpty(strTmp))
                        {
                            this.mailMessage.To.Add(new MailAddress(strTmp));
                        }
                    }
                }

                string[] strCc = cc.Split(c);
                if (strCc.Length > 0)
                {
                    foreach (string strTmp in strCc)
                    {
                        if (!string.IsNullOrEmpty(strTmp))
                        {
                            this.mailMessage.CC.Add(new MailAddress(strTmp));
                        }
                    }
                }

                this.mailMessage.Subject = subject;
                this.mailMessage.Body = body;

                if (!string.IsNullOrEmpty(re))
                {
                    this.mailMessage.Headers.Remove("Reply-To");
                    this.mailMessage.Headers.Add("Reply-To", re);
                }

                if (isHtml)
                {
                    this.mailMessage.IsBodyHtml = true;
                }
                else
                {
                    this.mailMessage.IsBodyHtml = false;
                }

                if (attachmentList != null && attachmentList.Count > 0)
                {
                    foreach (Attachment attachment in attachmentList)
                    {
                        this.mailMessage.Attachments.Add(attachment);
                    }
                }

                smtpClient.Send(mailMessage);
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion
    }
}
